#! /usr/bin/python2.4

# Copyright 2007 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""check_changebranch.py unittest"""

import unittest

import svn.fs

import testing.hooks.common
import gvn.hooks.info

from gvn.hooks.pre_commit.check_changebranch import RunHook


class TestCase(testing.hooks.common.HookTestCase):
  def testFoo(self):
    # Mock up a HookInfo.
    class mock_userdb:
      def UserInGroup(self, user, group):
        return False
    hi = gvn.hooks.info.HookInfo(['pre-commit', 'unused-repo', 'unused-txn'],
                                 userdb=mock_userdb())
    hi._author = 'basil'
    hi._head_root = object()
    hi._txn = object()
    # Mock svn.fs.node_prop for hi.project_config's gvn:project search.
    def mock_node_prop(root, path, propname, pool):
      return None
    class mock_path_change_t:
      change_kind = svn.fs.path_change_modify
    # Save the originals...
    orig_node_prop = svn.fs.node_prop
    try:
      # ...and install the mocks.
      svn.fs.node_prop = mock_node_prop
      # Check that we don't care about non-snapshot changes.
      hi._paths_changed = {'/trunk': mock_path_change_t()}
      self.assertEqual(RunHook(hi, self._logger), 0)
      # Reset for next test.
      hi._prefix_changed = None

      # Check that basil cannot create to sybil's changebranch area.
      hi._paths_changed = {'/changes/sybil': mock_path_change_t()}
      self.assertEqual(RunHook(hi, self._logger),
                       "basil cannot commit to sybil's changebranch area")
      # Reset for next test.
      hi._prefix_changed = None

      # Check that basil cannot commit to sybil's changebranch area.
      hi._paths_changed = {'/changes/sybil/foo': mock_path_change_t()}
      self.assertEqual(RunHook(hi, self._logger),
                       "basil cannot commit to sybil's changebranch area")
      # Reset for next test.
      hi._prefix_changed = None

      # Check that basil can create his own changebranch area.
      hi._paths_changed = {'/changes/basil': mock_path_change_t()}
      self.assertEqual(RunHook(hi, self._logger), 0)
      # Reset for next test.
      hi._prefix_changed = None

      # Check that basil can commit to his own changebranch area.
      hi._paths_changed = {'/changes/basil/foo': mock_path_change_t()}
      self.assertEqual(RunHook(hi, self._logger), 0)
    finally:
      svn.fs.node_prop = orig_node_prop
